package com.baidu.aip.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import java.io.ByteArrayOutputStream

/**
 *@packageName com.daai.manage.util
 *@author kzcai
 *@date 2020/4/3
 */
object ImageCompressBase64 {
    fun compressToBase64(imageData:ByteArray):String{
        val op = BitmapFactory.Options()
        op.inPreferredConfig = Bitmap.Config.RGB_565
        val bitmap = BitmapFactory.decodeByteArray(
                imageData,
                0,
                imageData.size,
                op
        )
        val bos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG,40,bos)
        bos.flush()
        bos.close()
        return Base64.encodeToString(bos.toByteArray(), Base64.NO_WRAP)
    }

    fun compressToBase64(imageData:Bitmap):String{
        val bos = ByteArrayOutputStream()
        imageData.compress(Bitmap.CompressFormat.JPEG,40,bos)
        bos.flush()
        bos.close()
        return Base64.encodeToString(bos.toByteArray(), Base64.NO_WRAP)
    }

    fun compressToByteArr(imageData:Bitmap):ByteArray{
        val bos = ByteArrayOutputStream()
        imageData.compress(Bitmap.CompressFormat.JPEG,40,bos)
        bos.flush()
        bos.close()
        return bos.toByteArray()
    }

    fun compressToBase64(imageData:Bitmap,quality:Int):String{
        val bos = ByteArrayOutputStream()
        imageData.compress(Bitmap.CompressFormat.JPEG,quality,bos)
        bos.flush()
        bos.close()
        return Base64.encodeToString(bos.toByteArray(), Base64.NO_WRAP)
    }
}